/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bgu.sonar.util.time;

import java.io.Serializable;
import java.util.Objects;
import java.util.logging.Logger;
import org.joda.time.DateTime;

/**
 *
 * @author bennyl
 */
public class TimeInterval implements Serializable{

    private DateTime from;
    private DateTime to;

    public TimeInterval(DateTime from, DateTime to) {
        this.from = from;
        this.to = to;
    }

    public TimeInterval() {
        this(DateTime.now(), DateTime.now());
    }

    public DateTime getFrom() {
        return from;
    }

    public DateTime getTo() {
        return to;
    }

    public void setFrom(DateTime from) {
        this.from = from;
    }

    public void setTo(DateTime to) {
        this.to = to;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + Objects.hashCode(this.from);
        hash = 59 * hash + Objects.hashCode(this.to);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final TimeInterval other = (TimeInterval) obj;
        if (!Objects.equals(this.from, other.from)) {
            return false;
        }
        if (!Objects.equals(this.to, other.to)) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "[" + from.toString() + " to " + to + ']';
    }

    public static TimeInterval valueOf(String string) {
        TimeInterval result = new TimeInterval();
        string = string.substring(0, string.length() - 1).substring(1); //remove []
        String[] strings = string.split(" to ");
        if (strings.length != 2) {
            throw new RuntimeException("bad format");
        }
        result.setFrom(new DateTime(strings[0]));
        result.setTo(new DateTime(strings[1]));
        return result;
    }
}
